package jco3.JCoTable;

import org.junit.Test;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoTable;

public class JCoTableAsImport {

	public JCoTable readTable() throws JCoException{
		 /**
         * Shows how to process JCoTable (as importing)
         */
		JCoDestination dest = JCoDestinationManager.getDestination("ECC");
		JCoFunction fm = dest.getRepository().getFunction("RFC_READ_TABLE");
		
		// table we want to query is USR04
        // which is user authorization table in SAP
		fm.getImportParameterList().setValue("QUERY_TABLE", "USR04");
		
		 // output data will be delimited by comma
		fm.getExportParameterList().setValue("DELIMITER", "");
		
		// processing table parameters
		JCoTable options = fm.getImportParameterList().getTable("OPTIONS");
		// modification date >= 2012.01.01 and <= 2015.12.31
		options.appendRow();
		options.setValue("TEXT", "MODDA GE '20120101' ");
		options.appendRow();
		options.setValue("TEXT", "AND MODDA LE '20151231' ");
		
		// We only care about fields of [user id] and [modification date]      
		String[] outputFields = new String[] {"BNAME", "MODDA"};
		JCoTable fields = fm.getTableParameterList().getTable("FIELDS");
		int count = outputFields.length;
		fields.appendRows(count);
		for(int i=0;i<count;i++) {
			fields.setRow(count);
			fields.setValue("FIELDNAME", outputFields[i]);
		}
		
		fm.execute(dest);
		JCoTable data = fm.getTableParameterList().getTable("DATA");
		return data;
	}
	
	@Test
	public void printUsers() throws JCoException{
		JCoTable users = this.readTable();
		JCoUtils.printJCoTable(users);
	}
}
